//
//  ChangePhoneViewController.m
//  LoveDraw
//
//  Created by 零零壹 on 2017/9/12.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "ChangePhoneViewController.h"

@interface ChangePhoneViewController ()
@property (weak, nonatomic) IBOutlet UITextField *oldPhoneTF;

@property (weak, nonatomic) IBOutlet UITextField *NewTelTF;


@property (weak, nonatomic) IBOutlet UITextField *YZMTF;

@property (weak, nonatomic) IBOutlet UIButton *SendMsg;

@property(nonatomic,assign)NSInteger index;

@end

@implementation ChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.oldPhoneTF.text = [UserInfoManage sharedInstance].userInfo.telphone;
    self.oldPhoneTF.userInteractionEnabled = NO;
    self.index = 60;
    [self setNavgation];

}

//设置导航
-(void)setNavgation{
    UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"nav_bg"]];
    UIView *navigationBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 64)];
    [navigationBar setBackgroundColor:bgColor];
    //左侧按钮
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 44, 44)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftOnClick) forControlEvents:UIControlEventTouchUpInside];
    [navigationBar addSubview:leftBtn];

    //中间信息
    UILabel *titLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, MAIN_SCREEN_WIDTH, 44)];
    [titLabel setTextColor:[UIColor whiteColor]];
    [titLabel setTextAlignment:1];
    [titLabel setText:@"变更手机号"];
    [navigationBar addSubview:titLabel];

    [self.view addSubview:navigationBar];
}

-(void)leftOnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)CommitAct:(id)sender
{
    if (self.NewTelTF.text.length == 0) {
        [HUD showText:@"请输入手机号码" AfterTime:1];
    }else if (self.YZMTF.text.length == 0)
    {
        [HUD showText:@"请输入验证码" AfterTime:1];
    }else
    {
        [RequestManager HudRequestWithUrl:@"/index.php?g=App&m=StudentInfo&a=alterTelphone" parmDic:@{@"telephone":self.NewTelTF.text,@"telephonecode":self.YZMTF.text} requestType:RequestPOST view:self.view Success:^(id data)
        {
            [UserInfoManage sharedInstance].userInfo.telphone = self.NewTelTF.text;
            [self.navigationController popViewControllerAnimated:YES];
        } error:^(NSError *error) {

        }];
    }
}

- (IBAction)SendYZM:(id)sender
{
    if (self.NewTelTF.text.length == 0) {
        [HUD showText:@"请先输入手机号码" AfterTime:1];
        return;
    }
    [RequestManager HudRequestWithUrl:@"/index.php?g=App&m=Login&a=appSendcode" parmDic:@{@"telephone":self.NewTelTF.text,@"UUID":[DeviceInfo getUUid],@"type":@"3"} requestType:RequestPOST view:self.view Success:^(id data)
     {
         NSTimer *timerAct = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(TimerHeadle:) userInfo:nil repeats:YES];
         [timerAct fire];
     } error:^(NSError *error)
     {

     }];
}

-(void)TimerHeadle:(NSTimer *)time;
{
    self.index--;
    self.SendMsg.enabled = NO;
    [self.SendMsg setTitle:[NSString stringWithFormat:@"%lds后发送",self.index] forState:(UIControlStateNormal)];
    self.SendMsg.backgroundColor = [UIColor grayColor];

    if (self.index == 0)
    {
        self.SendMsg.enabled = YES;
        self.SendMsg.backgroundColor = UIColors(0xFF8A2F);
        [self.SendMsg setTitle:@"获取验证码" forState:(UIControlStateNormal)];
        [time invalidate];
        self.index = 60;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
